package de.bazinga.rotw.weapon.pistol.bullet;

import java.util.Stack;
import java.util.Vector;

import com.jme.math.Vector3f;
import com.jme.renderer.ColorRGBA;
import com.jme.scene.Node;

import de.bazinga.rotw.person.Person;

public class BulletList {
	private Vector<Bullet> bullets = new Vector<Bullet>();

	private Stack<Bullet> unusedBullets = new Stack<Bullet>();
	
	private static BulletList instance = null;
	
	private BulletList() {
		
	}
	
	public static BulletList getInstance() {
		if(instance == null) instance = new BulletList();
		return instance;
	}
	
	public void addBullet(Bullet b) {
		bullets.add(b);
	}
	
	public void removeBullet(Bullet b) {
		bullets.remove(b);
	}
	
	public Vector<Bullet> getBullets() {
		return bullets;
	}
	
	public void removeAll() {
		bullets.removeAllElements();
	}

	
	
	public synchronized Bullet getUnusedBullet() {
		if(unusedBullets.size() == 0)  return null;
		
		return unusedBullets.pop();
	}
	
	
	public synchronized Bullet getUnusedBullet(Person person, Node rootNode, Vector3f location, Vector3f direction, ColorRGBA bulletColor) {
		Bullet bullet = BulletList.getInstance().getUnusedBullet();
		
		if(bullet == null) {
			bullet = new Bullet(rootNode, person, location, direction, bulletColor);
		}else{
			bullet.reset(person, location, direction, bulletColor);
			bullet.setActive(true);
		}
		
		return bullet;
	}
	
	public synchronized void addUnusedBullet(Bullet b) {
		unusedBullets.push(b);
	}
	
	public synchronized void clearUnusedBullets() {
		unusedBullets.clear();
	}
}
